<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->

        <el-tabs v-model="activeName">
          <el-tab-pane label="角色设置" name="first">
            <el-button type="primary" icon="el-icon-plus" @click="hAdd">添加</el-button>

            <el-table
              :data="roles"
              style="width: 100%"
            >
              <el-table-column
                label="序号"
                type="index"
              />
              <el-table-column
                label="角色名称"
                prop="name"
              />
              <el-table-column
                label="描述"
                prop="description"
              />
              <el-table-column
                align="right"
                label="操作"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="success"
                    @click="handleEdit(scope.$index, scope.row)"
                  >分配权限</el-button>
                  <el-button
                    size="mini"
                    type="primary"
                    @click="handleEdit(scope.$index, scope.row)"
                  >编辑</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!--
                :current-page 设置当前的页码
                page-sizes 可供选择的每页的条数
                page-size  当前每页的条数
                layout 决定布局元素的顺序
                total 一共有多少条数据

                @current-change: 页码变化时触发
                @size-change： 每页显示多少条 变化时触发
              -->
              <el-pagination
                :current-page="curPage"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="pageSize"
                layout="sizes, prev, pager, next, jumper, total"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />

            </el-row>
          </el-tab-pane></el-tabs>

      </el-card>
    </div>

    <!-- 弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit? '编辑' : '新增'"
      :visible.sync="isShowDialog"
      @close="hClose"
    >
      <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoles, delRole, addRole, updateRole } from '@/api/setting'
export default {
  data() {
    return {
      isEdit: false, // 是否编辑
      isShowDialog: false, // 是否显示弹框
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },

      roles: [], // 保存角色列表
      total: 0, // 总数量
      pageSize: 2, // 每页几条
      curPage: 1, // 当前是第几页
      activeName: 'first'
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hAdd() {
      // 显示弹框
      this.isShowDialog = true
      // 更新状态
      this.isEdit = false
    },
    async doAdd() {
      await addRole(this.roleForm)
      this.$message.success('添加成功')
      this.isShowDialog = false

      // 添加完成之后，直接跳入到最后一页
      // this.curPage = 最后一页的页码
      this.total++ // 总数+1
      this.curPage = Math.ceil(this.total / this.pageSize)
      this.loadRoles()
    },
    async doEdit() {
      await updateRole(this.roleForm)
      this.$message.success('编辑成功')
      this.isShowDialog = false
      this.loadRoles()
    },

    hSubmit() {
      // 做添加|编辑
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    async loadRoles() {
      const res = await getRoles(this.curPage, this.pageSize)
      console.log(res)
      this.roles = res.data.rows
      this.total = res.data.total
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      // 更新每页的条数
      this.curPage = 1
      this.pageSize = val
      // 重发请求
      this.loadRoles()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      // 更新页码
      this.curPage = val

      // 重发请求
      this.loadRoles()
    },

    handleEdit(index, row) {
      console.log(index, row)
      // 显示弹框
      this.isShowDialog = true
      // 更新状态
      this.isEdit = true
      // const obj = {}
      // obj.id = row.id
      // obj.description = row.description
      // obj.name = row.name
      // this.roleForm = obj
      // 数据回填
      this.roleForm = { ...row }
    },
    handleDelete(index, row) {
      console.log(index, row)
      const id = row.id
      this.$confirm('确认要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      await delRole(id)
      this.$message.success('删除成功')
      // if(删除的数据是表格中的最后一条数据) {
      //   把 页码--
      // }
      if (this.roles.length === 1) {
        this.curPage--
        // 处理 边界 情况
        if (this.curPage <= 0) {
          this.curPage = 1
        }
      }
      this.loadRoles()
    },
    hClose() {
      // 重置表单数据 - 清空表单校验结果
      this.$refs.roleForm.resetFields()

      this.roleForm.id = ''
      this.roleForm.name = ''
      this.roleForm.description = ''
    }
  }
}
</script>
